package com.forenow.demo.demo4;

public class Master {
    public void cure(Pet pet){
        if (pet.getHealth()<50) {
            pet.toHospital();
            System.out.println("吃药");
        }
    }
    public void play(Pet pet){
        if (pet instanceof Dog) {  // pet=Dog    true
            Dog d= (Dog) pet; //由小转大  强制转换
            d.run();
        }else if (pet instanceof Penguin){
            Penguin p= (Penguin) pet;
            p.swimming();
        }
    }
    public void feed(Pet pet){
        pet.eat();
    }

    public static void main(String[] args) {
        Master master=new Master();
        Pet p=new Dog();
        p.setName("花花");
        p.setHealth(40);
        p.setLove(99);
        p.eat();
        master.cure(p);
        master.play(p);
        p=new Penguin();
        p.setName("QQ");
        p.setHealth(20);
        p.setLove(100);
        master.cure(p);
        master.play(p);
        p.eat();
    }
}
